/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022.  All rights reserved.2023.
 */

package service

import (
	"errors"
	"fmt"
	"prim-server/common"
	"prim-server/dao"
	"prim-server/logger"
	"prim-server/model"
	"prim-server/util"
)

const (
	//云上产品
	producitonTypeOfCloud = "1"
	//平台自有产品
	productionTypeOfSelfOwned = "2"
)

// 判断产品是否为云上产品类型
func isCloudProduction(ProductionType string) bool {
	return ProductionType == producitonTypeOfCloud
}

// 判断产品是否为平台产品类型
func isSelfOwnedProduction(ProductionType string) bool {
	return ProductionType == productionTypeOfSelfOwned
}

// 增加平台产品
func AddProduction(addProduction []common.AddProductionInfo) error {

	errStr := ""
	errCount := 0
	for _, productionOne := range addProduction {
		production := &model.Production{
			ProductionId:   util.GetUUID(),
			TitleName:      productionOne.TitleName,
			ProductionType: productionOne.ProductionType,
			ProductionName: productionOne.ProductionName,
		}
		if isCloudProduction(productionOne.ProductionType) {
			production.IframeURL = productionOne.IframeURL
		} else {
			production.Price = productionOne.Price
		}
		err := dao.GetProductionDao().SaveProduction(production)
		if err != nil {
			logger.Error("save production error: %v, name: %s", err.Error(), production.ProductionName)
			errCount++
			errStr += fmt.Sprintf("[titlename:%s, name:%s, type:%s]",
				productionOne.TitleName,
				productionOne.ProductionName,
				productionOne.ProductionType)
		}
	}
	if errCount != 0 {
		return errors.New(errStr)
	}
	return nil
}

// 删除平台产品
func DeleteProduction(productionId string) error {
	if len(productionId) == 0 {
		logger.Error("productionId is empty")
		return errors.New("请求删除的产品ID为空")
	}
	err := dao.GetProductionDao().DeleteProduction(productionId)
	if err != nil {
		logger.Error("delete production error: %v, productionId: %s", err.Error(), productionId)
		return errors.New("删除产品失败")
	}
	return nil
}

// 查找平台产品
func GetProductionById(productionId string) (*model.Production, error) {
	products, err := GetProductionAll([]string{productionId}, nil, nil, nil, nil)
	if err != nil || len(products) == 0 {
		return nil, errors.New("不存在该产品")
	}
	return products[0], nil
}
func GetProductionAll(productionId, productionType, productionName []string, page, pageSize *int) ([]*model.Production, error) {
	queryParament := &common.QueryProductionDetail{
		ProductionId:   productionId,
		ProductionType: productionType,
		ProductionName: productionName,
		Page:           page,
		PageSize:       pageSize,
	}
	products, _, err := dao.GetProductionDao().GetProductions(queryParament)
	if err != nil {
		logger.Error("get production error: %v, productionId: %s, type: %s, name: %s", err.Error(), productionId, productionType, productionName)
		return nil, errors.New("查找产品目录失败")
	}
	return products, nil
}

// 构建查找平台产品的返回数据
func BuildReponseForGetProductionAll(products []*model.Production) *common.ProductionList {
	response := &common.ProductionList{
		TitalCount: len(products),
	}
	productionTitleNameMap := make(map[string]common.Productions)
	for _, product := range products {
		production := common.Production{
			ProductionId:   product.ProductionId,
			ProductionType: product.ProductionType,
			ProductionName: product.ProductionName,
			IframeURL:      product.IframeURL,
			Currency:       product.Currency,
			Price:          product.Price,
			TitleName:      product.TitleName,
		}
		tmpProductions := productionTitleNameMap[production.TitleName]
		tmpProductions.Production = append(tmpProductions.Production, production)
		tmpProductions.Size++
		tmpProductions.TitleName = production.TitleName
		productionTitleNameMap[production.TitleName] = tmpProductions
	}
	for _, proproductsMap := range productionTitleNameMap {
		response.Productions = append(response.Productions, proproductsMap)
	}
	return response
}
